package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.POJO.ShunDai;
import com.itheima.service.IShunDaiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zqystart
 * @create 2022-09-01-11:26
 */
@RestController
@RequestMapping("/shundai")
public class ShunDaiController {
    @Autowired
    private IShunDaiService shunDaiService;

    @GetMapping("/save")
    public R save(String username,String receiveAddress,String request,String send2Address,int money,Integer tel){//保存发布的顺带货物
        ShunDai shunDai=new ShunDai();
        shunDai.setUsername(username);
        shunDai.setReceiveAddress(receiveAddress);
        shunDai.setRequest(request);
        shunDai.setSend2Address(send2Address);
        shunDai.setMoney(money);
        shunDai.setTel(tel);
        boolean isSuccess = shunDaiService.save(shunDai);
        Map<String,Object> map=new HashMap<>();
        map.put("message",isSuccess);
        return R.ok(map);
    }

    @GetMapping("/selectAll")
    public R selectAll(){//查询所有的顺带货物订单
        List<ShunDai> list = shunDaiService.list();
//        Page<ShunDai> shunDaiPage = new Page<>(1,4);
//        Page<ShunDai> page = shunDaiService.page(shunDaiPage, new QueryWrapper<ShunDai>().orderByDesc("id"));
//        List<ShunDai> shunDaiList = page.getRecords();
        Map<String,Object> map=new HashMap<>();
        map.put("message",list);
        return R.ok(map);
    }

    @GetMapping("/select")
    public R select(Integer id){//通过id查询我发布的顺带货物  (可要可不要)
        ShunDai shunDai = shunDaiService.getById(id);
        Map<String,Object> map=new HashMap<>();
        map.put("message",shunDai);
        return R.ok(map);
    }

    @GetMapping("/selectByUsername")
    public R selectByUsername(String username){//通过用户名查询我发布的顺带货物
        List<ShunDai> shunDaiList = shunDaiService.list(new QueryWrapper<ShunDai>().eq("username", username));
        Map<String,Object> map=new HashMap<>();
        map.put("message",shunDaiList);
        return R.ok(map);
    }

    @GetMapping("/delete")
    public R delete(Integer id){//通过id删除顺带货物订单
        boolean remove = shunDaiService.removeById(id);
        Map<String,Object> map=new HashMap<>();
        map.put("message",remove);
        return R.ok(map);
    }

    @RequestMapping(value = "/uploadImage",method = RequestMethod.POST)
    public R uploadImage(String filePath,String name){//接受处理图片的方法   (暂时无法实现)
        System.out.println(filePath);
        System.out.println(name);
        Map<String,Object> map=new HashMap<>();
        map.put("message",filePath);
        return R.ok(map);
    }
}
